<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['xm:itemInfo:add']"
          v-if="showInoutAdd && optType != 'view'"
        >新增</el-button>
      </el-col>
    </el-row>

    <el-table border v-loading="loading" :data="itemInfoList" @selection-change="handleSelectionChange">
      <el-table-column label="编号" align="center" prop="itemCode" width="50px">
        <template slot-scope="scope">
          <el-button
            type="text"
            @click="handleView(scope.row)"
            v-hasPermi="['xm:projectInfo:query']"
          >{{ scope.row.itemCode }}
          </el-button>
        </template>
      </el-table-column>
      <el-table-column label="名称" align="center" prop="name" />
      <el-table-column label="版本" align="center" prop="version" />
      <el-table-column label="状态" align="center" prop="status"width="80px" >
        <template slot-scope="scope">
          <dict-tag :options="dict.type.project_item_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="创建人" align="center" prop="createBy" width="80px"/>
      <el-table-column label="创建时间" align="center" prop="createTime"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['xm:itemInfo:edit']"
            v-if="showInoutAdd  && optType != 'view'"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['xm:itemInfo:remove']"
            v-if="showInoutAdd && optType != 'view'"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改项目输入输出信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="编号" prop="itemCode">
          <el-input v-model="form.itemCode" placeholder="请输入编号" :readonly="inoptType == 'view'"/>
        </el-form-item>
        <el-form-item label="名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入名称" :readonly="inoptType == 'view'"/>
        </el-form-item>
        <el-form-item label="版本" prop="version">
          <el-input v-model="form.version" placeholder="请输入版本" :readonly="inoptType == 'view'"/>
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-select v-model="form.status" placeholder="请选择状态" :disabled="inoptType == 'view'" @change="handleChange" clearable>
            <el-option
              v-for="dict in dict.type.project_item_status"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="附件" prop="fileId">
          <FileUpload
            :limit="100"
            :fileSize="100"
            :fileType="fileType"
            :value="fileList"
            :optType="inoptType"
            :isShowTip="false"
            @onUploaded="handleUploaded"
            @onRemoved="handleRemoved"
            :disabled="inoptType === 'view'"
          >
          </FileUpload>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm" v-if="inoptType != 'view'">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listItemInfo, getItemInfo, delItemInfo, addItemInfo, updateItemInfo } from "@/api/mes/xm/itemInfo";
import {getProjectInfo} from "@/api/mes/xm/projectInfo";

export default {
  name: "ItemInfo",
  dicts: ['project_item_status'],
  props: {
    projectId: {
      type: [Number],
      required: true
    },
    projectName:{
      type: [String],
      required: true
    },
    templateId:{
      type: [Number],
      required: true
    },
    templateName:{
      type: [String],
      required: true
    },
    itemType:{
      type: [Number],
      required: true
    },
    showInoutAdd:{
      type: [Boolean],
      required: true
    },
    optType:{
      type: [String],
      required: true
    }
  },
  data() {
    return {
      fileType: [
        // 文档类
        "pdf", "doc", "docx", "xls", "xlsx",
        // 图片类
        "jpg", "jpeg", "png", "gif", "bmp",
        // 视频类
        "mp4", "avi", "rmvb", "mkv", "mov",
        // 压缩文件类
        "zip", "rar", "7z", "tar", "gz"
      ],
      fileList: [],
      inoptType: null,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 项目输入输出信息表格数据
      itemInfoList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        itemCode: null,        name: null,        version: null,        itemType: null,        status: null,        statusName: null,        permissions: null,        permissionsName: null,        projectId: null,        projectName: null,        fileId: null,        remarks: null,      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        itemCode: [
          { required: true, message: "编号不能为空", trigger: "blur" }
        ],        name: [
          { required: true, message: "名称不能为空", trigger: "blur" }
        ],status:[
          { required: true, message: "请选择状态", trigger: "blur" }
        ]      }
    };
  },
  created() {
    this.getList();
  },
  watch: {
    optType(newVal) {
      if (newVal) {
        this.optType=newVal;
      }
    },
    projectId(newVal) {
      if (newVal) {
        this.projectId=newVal;
        this.getList();
      }
    },
    itemType(newVal) {
      if (newVal) {
        this.itemType=newVal;
        this.getList();
      }
    },
    templateId(newVal) {
      if (newVal) {
        this.templateId=newVal;
        this.getList();
      }
    },
    templateName(newVal) {
      if (newVal) {
        this.templateName=newVal;
      }
    },
    showInoutAdd(newVal){
      if (newVal) {
        this.showInoutAdd=newVal;
      }
    }
  },
  methods: {
    handleView(row) {
      this.reset();
      const id = row.id || this.ids
      getItemInfo(id).then(response => {
        this.fileList.push(response.data.fileId);
        this.form = response.data;
        this.open = true;
        this.inoptType = 'view';
        this.title = "查看信息";
      });
    },
    // 上传成功回调
    handleUploaded(data) {
      this.form.fileId = data;
    },
    // 删除成功回调
    handleRemoved(data) {
      this.form.fileId = data;
    },
    handleChange(val) {
      this.form.statusName = this.dict.type.project_item_status.find(item => item.value == val).label;
    },
    /** 查询项目输入输出信息列表 */
    getList() {
      this.loading = true;
      this.queryParams.itemType=this.itemType;
      this.queryParams.projectId=this.projectId;
      this.queryParams.permissions=this.templateId;
      listItemInfo(this.queryParams).then(response => {
        this.itemInfoList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,        itemCode: null,        name: null,        version: null,        itemType: null,        status: null,        statusName: null,        permissions: null,        permissionsName: null,        projectId: null,        projectName: null,        fileId: null,        remarks: null,        createBy: null,        createTime: null,        updateBy: null,        updateTime: null,        tenantId: null      };
      this.fileList=[];
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.inoptType = 'add';
      this.title = "添加项目输入输出信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getItemInfo(id).then(response => {
        this.fileList.push(response.data.fileId);
        this.form = response.data;
        this.open = true;
        this.inoptType = 'edit';
        this.title = "修改项目输入输出信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.itemType = this.itemType;
          this.form.projectId = this.projectId;
          this.form.projectName = this.projectName;
          this.form.permissions = this.templateId;
          this.form.permissionsName = this.templateName;
          if (this.form.id != null) {
            updateItemInfo(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addItemInfo(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除项目输入输出信息编号为"' + ids + '"的数据项？').then(function() {
        return delItemInfo(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('xm/itemInfo/export', {
        ...this.queryParams
      }, `itemInfo_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
